#coding: latin-1
from sqlalchemy import create_engine
from sqlalchemy.sql.expression import text
from datetime import datetime
print 'Starting engine ...'
engine = create_engine('sqlite:///database.db', echo=False)

#from sqlalchemy.orm import sessionmaker
#Session = sessionmaker(bind=engine)
#session = Session()

FORMAT_DATE = '%d/%m/%Y'
FORMAT_DATE_TIME = '%d/%m/%Y - %H:%M:%S'
from sqlalchemy import Table,       \
                       Column,      \
                       Integer,     \
                       String,      \
                       Numeric,     \
                       MetaData,    \
                       Date,        \
                       DateTime,    \
                       ForeignKey   

from sqlalchemy.ext.declarative import declarative_base
import util
Base = declarative_base()
class Util:
    def __init__(self, *args, **wargs):
        pass
    def get_date_format(self, column):
        if type(column.type) is Date:
            format_date = FORMAT_DATE
        elif type(column.type) is DateTime:
            format_date = FORMAT_DATE_TIME
        name_column = column.name
        attr = getattr(self,  name_column)
        return  attr and datetime.strftime(attr, format_date) or attr
    
    def get_value_by_column(self, column):
        is_date = util.is_date(column)
        name_column = column.name
        if is_date:
            return self.get_date_format(column)
        return getattr(self, name_column)

class Transacao(Base, Util):
    __tablename__ = 'tab_transacao'
    id          = Column(Integer, primary_key=True)
    numero_cartao       = Column(String(20), nullable=False)
    data_transacao      = Column(DateTime, nullable=False)
    tipo_transacao      = Column(String(50), nullable=False)
    
    moeda               = Column(String(10), default='REAL')
    valor               = Column(Numeric(precision=2, scale=2, asdecimal=True), default=0.0)
    
    estabelecimento     = Column(String(50), nullable=True)
    localizacao         = Column(String(50), nullable=True)
    estado              = Column(String(10), nullable=True)
    pais                = Column(String(20), nullable=True)
    
    
    def __init__(self, *args, **wargs):
        Base.__init__(self, *args, **wargs)
        Util.__init__(self, *args, **wargs)

    def __repr__(self):
       return "<MyTransacao('%s','%s')>" % (self.tipo_transacao, self.valor)

class Despesa(Base, Util):
    __tablename__ = 'tab_despesa'
    id          = Column(Integer, primary_key=True)
    titulo      = Column(String(50), nullable=False)
    descricao   = Column(String(512), default="descricao")
    valor       = Column(Numeric(precision=2, scale=2, asdecimal=True), default=0.0)
    vencimento  = Column(Date(), nullable=True)
    
    
    def __init__(self, *args, **wargs):
        Base.__init__(self, *args, **wargs)
        Util.__init__(self, *args, **wargs)

    def __repr__(self):
       return "<MyDespesa('%s','%s')>" % (self.titulo, self.valor)


class Recurso(Base, Util):
    __tablename__ = 'tab_recurso'
    
    def __init__(self, *args, **wargs):
        Base.__init__(self, *args, **wargs)
        Util.__init__(self, *args, **wargs)

    id          = Column(Integer, primary_key=True)
    titulo      = Column(String(50), nullable=False)
    descricao   = Column(String(512), default="descricao")
    valor       = Column(Numeric(precision=2, scale=2, asdecimal=True), default=0.0)
    vencimento  = Column(Date(), nullable=True)
    

    def __repr__(self):
       return "<MyRecurso('%s','%s')>" % (self.titulo, self.valor)

class Template(Base, Util):
    __tablename__ = 'tab_template'

    def __init__(self, *args, **wargs):
        Base.__init__(self, *args, **wargs)
        Util.__init__(self, *args, **wargs)

    id              = Column(Integer, primary_key=True)
    titulo          = Column(String(50), nullable=False)
    descricao       = Column(String(512), default="descricao")
    valor           = Column(Numeric(precision=2, scale=2, asdecimal=True), default=0.0)
    vencimento      = Column(Date(), nullable=True)
    calculo_juros   = Column(String(1024), default='')
    calculo_valor   = Column(String(1024), default='')
    referencia      = Column(String(8), default='') #\d\d \/ \d\d /\ \d\d #data de referecnia
    
    def __repr__(self):
       return "<MyTemplate('%s','%s')>" % (self.titulo, self.valor, self.referencia)

metadata = Base.metadata
metadata.create_all(engine)
#a = Despesa(titulo='aaa', descricao='abra')
#session.add(a)
#session.commit()
#print session.query(Despesa).all()[-1].descricao
#edit_despesa = session.query(Despesa).filter_by(id=1).one() 
#print edit_despesa.id
